package leetcode.Leetcode238;

class Solution {
    public int[] productExceptSelf(int[] nums) {
        //构建该数左边和右边所有元素的乘积，使用两个数组保存
        int len = nums.length;
        int[] ans = new int[len];
        int[] lArray = new int[len];
        int[] rArray = new int[len];
        lArray[0] = 1;
        rArray[len-1] = 1;
        for (int i = 1,j = len - 2; i < len; i++,j--) {
            lArray[i]=lArray[i-1]*nums[i-1];
            rArray[j]=rArray[j+1]*nums[j+1];
        }
        for (int i = 0; i < len; i++) {
            ans[i]=lArray[i]*rArray[i];
        }
        return ans;
    }
}